﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace scomA2Proj
{
    public class OrderReciept
    {
        public enum OrderResult { Success, FailCC };
        //auto increments per order.
        private static int orderNum = 0;

        //Base Quantities
        public Stopwatch stopwatch;
        public int OrderNumber { get; private set; }
        public int OrdererID { get; private set; }
        public int Amount { get; private set; }
        public decimal PricePerChicken { get; private set; }
        public decimal TaxRate { get; private set; }
        public decimal ShippingCostPerChicken { get; private set; }
        public OrderResult Result { get; private set; }


        //Derived Quantities
        public decimal Total
        {
            get
            {
                return Subtotal + TaxTotal + ShippingTotal;
            }
        }
        public decimal Subtotal
        {
            get
            {
                if (Result == OrderResult.FailCC) return 0;
                return Amount * PricePerChicken;
            }
        }
        public decimal TaxTotal { get { return Subtotal * TaxRate; } }
        public decimal ShippingTotal
        {
            get
            {
                if (Result == OrderResult.FailCC) return 0;
                return ShippingCostPerChicken * Amount;
            }
        }

        /// <summary>
        /// Constructor for Order receipt. 
        /// </summary>
        /// <param name="dt">The order date time</param>
        /// <param name="ordererID">The orderer's id</param>
        /// <param name="amount">Number of chickens</param>
        /// <param name="pricePerChicken">Price per chicken.</param>
        /// <param name="taxRate">Tax rate, eg .09 is 9%</param>
        /// <param name="shippingCostPerChicken">The cost of shipping per chicken.</param>
        public OrderReciept(Stopwatch stopwatch, int ordererID, int amount, decimal pricePerChicken, decimal taxRate, decimal shippingCostPerChicken, OrderResult oResult)
        {
            //lock the static variables
            lock (typeof(OrderReciept))
            {
                this.OrderNumber = orderNum;
                orderNum++;
            }
           
            this.stopwatch = stopwatch;
            this.OrdererID = ordererID;
            this.Amount = amount;
            this.PricePerChicken = pricePerChicken;
            this.TaxRate = taxRate;
            this.ShippingCostPerChicken = shippingCostPerChicken;
            this.Result = oResult;
        }

        /// <summary>
        /// Returns the receipt info human readable format.
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            StringBuilder b = new StringBuilder();
            b.AppendLine("---Chicken Farm Receipt---");
            b.AppendLine("Order Number: " + OrderNumber);
            b.AppendLine("Ordered By: " + OrdererID);
            b.AppendLine("-------------------------------");
            if (Result == OrderResult.Success)
            {
                b.AppendLine("Order Amount: " + Amount);
                b.AppendLine("\t@ $" + String.Format("{0:0.00}", PricePerChicken) + " ea");
                b.AppendLine("Subtotal: $" + String.Format("{0:0.00}", Subtotal));
                b.AppendLine("Tax(" + TaxRate + "%): $" + String.Format("{0:0.00}", TaxTotal));
                b.AppendLine("Shipping: $" + String.Format("{0:0.00}", ShippingTotal));
                b.AppendLine("-------------");
                b.AppendLine("Total: $" + String.Format("{0:0.00}", Total));
            }
            else if (Result == OrderResult.FailCC)
            {
                b.Append("Order failed. Invalid credit card number.");
            }
            else
            {
                throw new Exception("Invalid Order Result.");
            }
            return b.ToString();
        }
    }
}
